﻿using System.Collections.Generic;
using Cms.Application.Admin.Views;
using Cms.Application.IO;
using Cms.Data;
using Cms.Domain;
using Cms.Domain.RepositoryInterfaces;

namespace Cms.Application.Admin.Presenters
{
    public class DetailTemplatesPresenter : BasePresenter
    {
        private readonly Administrator _administrator;
        private readonly IDetailTemplatesView _view;

        public IDiskAccess DiskAccess { get; set; }
        
        public DetailTemplatesPresenter(IDetailTemplatesView view)
            : this(view, new RepositoryFactory())
        {
            DiskAccess = new DiskAccess();
        }
        
        public DetailTemplatesPresenter(IDetailTemplatesView view, IRepositoryFactory repositoryFactory)
            : base(view, repositoryFactory)
        {
            _view = view;
            _administrator = AdministratorRepository.GetAdministrator(_view.AdministratorID);
            if (!_administrator.HasPermission.DetailTemplates)
                _view.RedirectToPermissionDeniedPage();
        }

        public void Initialize()
        {
            SetDetailTemplates();
            if (_administrator.CurrentSite.MasterTemplates.Count == 0)
                _view.DisplayCreateMasterTemplateAlert();
        }

        public void SetDetailTemplates()
        {
            _view.SetDetailTemplates(_administrator.CurrentSite.DetailTemplates);
        }
        
        public void DeleteDetailTemplate(int id)
        {
            DetailTemplate dt = SiteRepository.GetDetailTemplate(id);
            if (dt.Pages.Count == 0)
            {
                PerformDelete(dt);
            }
            else if (_administrator.CurrentSite.DetailTemplates.Count > 1)
            {
                //Let user select an alternative detailtemplate for the associated pages
                IList<DetailTemplate> replacements = new List<DetailTemplate>();
                foreach (DetailTemplate replacement in _administrator.CurrentSite.DetailTemplates)
                    if (replacement.ID != id)
                        replacements.Add(replacement);
                _view.SetReplacements(replacements, dt.Name);
            }
            else
            {
                _view.DisplayUnableToDeleteAlert(dt.Name);
            }
        }
        
        public void ReplaceAndDeleteDetailTemplate(int id, int replacementID)
        {
            DetailTemplate dt = SiteRepository.GetDetailTemplate(id);
            DetailTemplate replacement;
            while (dt.Pages.Count > 0)
            {
                Page p = dt.Pages[0];
                replacement = SiteRepository.GetDetailTemplate(replacementID);
                p.DetailTemplate = replacement;
                replacement.Pages.Add(p);
                dt.Pages.Remove(p);
            }
            SiteRepository.SaveSite(_administrator.CurrentSite);
            PerformDelete(dt);
        }

        private void PerformDelete(DetailTemplate dt)
        {
            dt.Site.DetailTemplates.Remove(dt);
            if (dt.Site.DefaultDetailTemplate.ID == dt.ID)
            {
                dt.Site.DefaultDetailTemplate = dt.Site.DetailTemplates.Count > 0 ? dt.Site.DetailTemplates[0] : null;
            }
            dt.Site = null;
            foreach (DetailTemplateContentLink dtl in dt.Content)
            {
                dtl.DetailTemplate = null;
            }
            dt.Content.Clear();
            SiteRepository.DeleteDetailTemplate(dt.ID);
            SiteRepository.SaveSite(_administrator.CurrentSite);
            DiskAccess.DeleteFile("__System\\DetailTemplates\\" + dt.ID + ".master");
            SetDetailTemplates();
        }
    }
}